// 云函数入口文件
const mysql = require('mysql2/promise')

// MySQL连接配置
const config = {
  host: 'sh-cynosdbmysql-grp-b4b8nkho.sql.tencentcdb.com',
  port: 25932,
  database: 'custom_order_management',
  user: 'root',
  password: 'jQ6w2uwY',
  // 添加连接选项
  connectTimeout: 10000, // 连接超时时间10秒
  connectionLimit: 10,   // 连接池限制
  waitForConnections: true,
  queueLimit: 0
}

// 云函数入口函数
exports.main = async (event, context) => {
  let connection
  try {
    console.log('开始测试数据库查询...')
    
    // 创建数据库连接
    connection = await mysql.createConnection(config)
    
    // 查询member表
    console.log('查询member表...')
    const [members] = await connection.execute('SELECT * FROM member')
    console.log('member表数据:', JSON.stringify(members))
    
    // 查询merchant_info表
    console.log('查询merchant_info表...')
    const [merchants] = await connection.execute('SELECT * FROM merchant_info')
    console.log('merchant_info表数据:', JSON.stringify(merchants))
    
    return {
      success: true,
      data: {
        members,
        merchants
      }
    }
    
  } catch (error) {
    console.error('数据库查询失败:', error)
    return {
      success: false,
      error: error.message
    }
  } finally {
    if (connection) {
      await connection.end()
    }
  }
} 